﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Validity.Test.Constraints.StringConstraints
{
  [TestClass]
  public class EndsWithConstraint
    : ConstraintTestBase<ObjectStub>
  {
    public class Validator
      : ValidatorBase<ObjectStub>
    {
      public Validator()
      {
        const String endsWith = "-END";

        DefineRulesFor(ctx => ctx.MyStringProperty1).EndsWith(endsWith);
        DefineRulesFor(ctx => ctx.MyStringProperty2).EndsWith(() => endsWith);
        DefineRulesFor(ctx => ctx.MyStringProperty3).EndsWith(obj => endsWith);
      }
    }

    public EndsWithConstraint() : base(new Validator(), 3) { }

    [TestMethod]
    public void TestConstraintVerified()
    {
      ValidateConstraintVerfied(CreateContext("START-END"));
    }

    [TestMethod]
    public void TestConstraintViolated()
    {
      const String ruleViolation = "Must end with '-END'";

      ValidateConstraintViolated(CreateContext(null), ruleViolation);
      ValidateConstraintViolated(CreateContext(String.Empty), ruleViolation);
      ValidateConstraintViolated(CreateContext("START-END "), ruleViolation);
      ValidateConstraintViolated(CreateContext("START-END_"), ruleViolation);
      ValidateConstraintViolated(CreateContext("END-START"), ruleViolation);
      ValidateConstraintViolated(CreateContext("OTHER"), ruleViolation);
    }

    public ObjectStub CreateContext(String value)
    {
      var result = new ObjectStub();

      result.MyStringProperty1 = value;
      result.MyStringProperty2 = value;
      result.MyStringProperty3 = value;

      return result;
    }
  }
}
